﻿using B.S.Medical.Api.Read.Application.Command.Department;
using B.S.Medical.Api.Read.DTOS;
using B.S.Medical.Domain.Department;
using B.S.Medical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Medical.Api.Read.Controllers.Department
{
    /// <summary>
    /// 科室管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator mediator;

        public DepartmentController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        
        /// <summary>
        /// 科室显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPageing<ManageModelDTO>> Handle([FromQuery ]ManagePageCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request);
        }
        
        /// <summary>
        /// 科室树
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<DepartmentTreeDTO>>> DepartmentTree([FromQuery]DepartmentTreeCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        
        /// 获取所有科室列表（级联结构）
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns>返回科室的级联结构</returns>
        [HttpGet]
        public Task<ApiResult<List<DepartmentCascadeDTO>>> GetAllDepartments(CancellationToken cancellationToken)
        {
            return mediator.Send(new GetAllDepartmentsCommand(), cancellationToken);
        }
        
        /// <summary>
        /// 根据院区ID获取科室列表
        /// </summary>
        /// <param name="campusId">院区ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>科室列表</returns>
        [HttpGet]
        public Task<ApiResult<List<DepartmentListDTO>>> GetDepartmentsByCampus(int campusId, CancellationToken cancellationToken)
        {
            return mediator.Send(new GetDepartmentByCampusCommand { CampusId = campusId }, cancellationToken);
        }

        /// <summary>
        /// 根据院区ID获取科室级联结构
        /// </summary>
        /// <param name="campusId">院区ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>科室级联结构</returns>
        [HttpGet]
        public Task<ApiResult<List<DepartmentCascadeDTO>>> GetDepartmentCascadeByCampus(int campusId, CancellationToken cancellationToken)
        {
            return mediator.Send(new GetDepartmentCascadeByCampusCommand { CampusId = campusId }, cancellationToken);
        }

        /// <summary>
        /// 根据医生ID获取科室信息
        /// </summary>
        /// <param name="doctorId">医生ID</param>
        /// <returns>科室信息</returns>
        [HttpGet("{doctorId}")]
        public async Task<ApiResult<ManageModelDTO>> GetDepartmentByDoctorId(int doctorId)
        {
            var command = new GetDepartmentByDoctorIdCommand { DoctorId = doctorId };
            return await mediator.Send(command);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<DepartmentDaoChuDto>>> DaoChu([FromQuery]DaoChuCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 科室导出Excel文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet("ExportExcel")]
        public async Task<IActionResult> ExportExcel([FromQuery]DaoChuCommand request, CancellationToken cancellationToken)
        {
            var result = await mediator.Send(request, cancellationToken);
            var data = result.Data ?? new List<DepartmentDaoChuDto>();
            byte[] fileBytes;
            using (var package = new OfficeOpenXml.ExcelPackage())
            {
                // EPPlus 5/6/7 LicenseContext写法
                OfficeOpenXml.ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
                var worksheet = package.Workbook.Worksheets.Add("Departments");
                worksheet.Cells[1, 1].Value = "Id";
                worksheet.Cells[1, 2].Value = "Department Name";
                worksheet.Cells[1, 3].Value = "Campus Id";
                worksheet.Cells[1, 4].Value = "Parent Id";
                worksheet.Cells[1, 5].Value = "Department Address";
                worksheet.Cells[1, 6].Value = "Status";
                for (int i = 0; i < data.Count; i++)
                {
                    worksheet.Cells[i + 2, 1].Value = data[i].Id;
                    worksheet.Cells[i + 2, 2].Value = data[i].DepartmentName;
                    worksheet.Cells[i + 2, 3].Value = data[i].CampusId;
                    worksheet.Cells[i + 2, 4].Value = data[i].ParetId;
                    worksheet.Cells[i + 2, 5].Value = data[i].DepartmentAddr;
                    worksheet.Cells[i + 2, 6].Value = data[i].Status;
                }
                fileBytes = package.GetAsByteArray();
            }
            return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Departments.xlsx");
        }
        /// <summary>
        /// 日志显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPageing<LogModel>> Log([FromQuery]LogCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request);
        }
    }
}
